var requestCommandsArr:Array=new Array();
var seqId:Number=0;
// var requestCommandItem:Object;
// 2008-11-21 added the executeQueue
var executeQueue:Array=new Array();

function request(type:String, method:String, params:Object, viewObj:MovieClip, viewMode:String, responser:MovieClip, data)
{
	Packages.log(this._name+".request");
	// call related services
	// type : "HTTP" - HTTPRequest ; "REMOTING" - RemotingCall ;
	// method : special function
	// params : params for the method
	// responser : the responser of the request command
	// viewObj : the view object of the request result data
	// viewMode : the view mode of the request result data
	// data : the data of the target

	// register request object
	var requestCommandItem:Object=new Object();

	requestCommandItem.service=new Object();
	requestCommandItem.service.type=type;
	requestCommandItem.service.method=method;
	// added sessionId property for the HttpRequest and FlashRemoting
	if (Packages.model.Context.sessionID!=null) 
	{
		if (params==null) params=new Object();
		params.sessionId=Packages.model.Context.sessionID;
	}
	requestCommandItem.service.params=params;
	requestCommandItem.vo=new Object();
	requestCommandItem.vo.instance=viewObj;
	requestCommandItem.vo.mode=viewMode;
	requestCommandItem.component=new Object();
	requestCommandItem.component.responser=responser;
	requestCommandItem.component.data=data;

	requestCommandsArr[++seqId]=requestCommandItem;

	// 2008-11-21 added the executeQueue
	// launch(requestCommandItem, seqId);
	if (executeQueue.length==0)
	{
		executeQueue.push({req:requestCommandItem, seq:seqId});
		launchQueue();
	}
}

function launchQueue()		// 2008-11-21 added the executeQueue
{
	var executeObj=executeQueue.shift();
	launch(executeObj.req, executeObj.seq);
}

function launch(requestCommandObj:Object, index:Number)
{
	Packages.log(this._name+".launch");

	if (requestCommandObj.service.type=="HTTP")
	{
		Packages.services.HTTPRequest.execute(requestCommandObj.service, this, index);
	}
	else if (requestCommandObj.service.type=="REMOTING")
	{
		Packages.services.RemotingCall.execute(requestCommandObj.service, this, index);
	}
}

function handleRequestResult(rst:Object)
{
	Packages.log(this._name+".handleRequestResult");
	
	if (rst.index!=null)
	{
		var requestCommandItem=requestCommandsArr[rst.index];
		if (rst.data!=null)
		{
			requestCommandItem.service.result=rst.data;
			var args:Array=new Array();
			args.push(rst.data);
			if (requestCommandItem.component.data!=null) args.push(requestCommandItem.component.data);
			
			if (requestCommandItem.vo.instance!=null)
			{
				var voData=requestCommandItem.vo.instance[requestCommandItem.vo.mode].apply(requestCommandItem.vo.instance, args);

				requestCommandItem.vo.instance=voData;
			}
		}
		
		if (Packages.model.Context.activeContent==requestCommandItem.component.responser) requestCommandItem.component.responser.handleResponseResult(requestCommandItem);
		else
		{
			requestCommandItem.vo=null;
			delete requestCommandItem.vo;
			requestCommandItem.component.responser.handleResponseResult(requestCommandItem);
		}

		delete requestCommandItem;
		requestCommandsArr[rst.index]=null;
		delete requestCommandsArr[rst.index];
	}

	// 2008-11-21 added the executeQueue
	if (executeQueue.length>0)
	{
		launchQueue();
	}
}